package com.wwm.petadopt;

import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ClassificationFragment extends Fragment
{
	HorizontalListView hListViewDog;
	HorizontalListView hListViewCat;
	HorizontalListViewAdapter hListViewAdapterCat;
	HorizontalListViewAdapter hListViewAdapterDog;
	private RequestQueue mRequestQueue;
	private static android.app.FragmentManager fragmentManager;
	@Override
	public void onStart()
	{
		super.onStart();
		ActionBar ab = getActivity().getActionBar();
		if (ab != null)
		{
			View actionbarview=getActivity().getLayoutInflater().inflate(R.layout.actionbar,null);
			((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.classification);

			ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
			ab.setCustomView(actionbarview);
			Log.i("actionbar", "AccountFragment");
		}
	}

	@Override
	public void onResume()
	{
		super.onResume();
		ActionBar ab = getActivity().getActionBar();
		if (ab != null)
		{
			View actionbarview=getActivity().getLayoutInflater().inflate(R.layout.actionbar,null);
			((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.classification);

			ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
			ab.setCustomView(actionbarview);
			Log.i("actionbar", "AccountFragment");
		}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_classification, container, false);
		ActionBar ab = getActivity().getActionBar();
		if (ab != null)
		{

			Log.i("actionbar", "ClassificationFragment");
		}
		fragmentManager=getActivity().getFragmentManager();
		hListViewDog = (HorizontalListView) view.findViewById(R.id.horizontal_listview_dog);



		String[] dog_titles =
		{ "123狗图片1", "123狗图片2", "123狗图片3" , "123狗图片4", "123狗图片5", "123狗图片6"};
		final int[] dog_ids =
		{ R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher };
		hListViewAdapterDog = new HorizontalListViewAdapter(view.getContext(), dog_titles, dog_ids);
		mRequestQueue = Volley.newRequestQueue(view.getContext());
		Map<String, String> map = new HashMap<String, String>();
		map.put("RequestNum", "6");
		map.put("RequestNumType", "dog");

		JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, GlobalData.GET_PET_INFO_WITH_NUM_URL, new JSONObject(map), new Response.Listener<JSONObject>()
		{
			@Override
			public void onResponse(JSONObject response)
			{
				Log.i("petadopt", response.toString());

				ArrayList<PetDetail> petDetailArray = new ArrayList<PetDetail>();

				try {
					Iterator it = response.keys();
					while (it.hasNext()) {
						String key = (String) it.next();
						String value = response.getString(key);
						JSONObject j = response.getJSONObject(key);
						PetDetail p = new PetDetail(j);
						petDetailArray.add(p);
					}

					hListViewAdapterDog.setData(petDetailArray);
					hListViewAdapterDog.notifyDataSetChanged();

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		}, new Response.ErrorListener()
		{
			@Override
			public void onErrorResponse(VolleyError error)
			{
				Log.i("petadopt", error.getMessage());
			}
		});
		mRequestQueue.add(jr);
		hListViewDog.setAdapter(hListViewAdapterDog);

		hListViewDog.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				hListViewAdapterDog.setSelectIndex(position);
				hListViewAdapterDog.notifyDataSetChanged();
				PetDetail petDetail = (PetDetail)parent.getItemAtPosition(position);

				Bundle bundle=new Bundle();
				bundle.putString("petId", petDetail.petId);

				Intent intent=new Intent("android.intent.action.PETDETAIL");
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		
		hListViewCat = (HorizontalListView) view.findViewById(R.id.horizontal_listview_cat);

		hListViewAdapterCat=new HorizontalListViewAdapter(view.getContext(), dog_titles, dog_ids);
		Map<String, String> mapcat = new HashMap<String, String>();
		mapcat.put("RequestNum", "6");
		mapcat.put("RequestNumType", "cat");

		 jr = new JsonObjectRequest(Request.Method.POST, GlobalData.GET_PET_INFO_WITH_NUM_URL, new JSONObject(mapcat), new Response.Listener<JSONObject>()
		{
			@Override
			public void onResponse(JSONObject response)
			{
				Log.i("petadopt", response.toString());

				ArrayList<PetDetail> petDetailArray = new ArrayList<PetDetail>();


				try {
					Iterator it = response.keys();
					while (it.hasNext()) {
						String key = (String) it.next();
						String value = response.getString(key);
						JSONObject j = response.getJSONObject(key);
						PetDetail p = new PetDetail(j);
						petDetailArray.add(p);
					}

					hListViewAdapterCat.setData(petDetailArray);
					hListViewAdapterCat.notifyDataSetChanged();

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		}, new Response.ErrorListener()
		{
			@Override
			public void onErrorResponse(VolleyError error)
			{
				Log.i("petadopt", error.getMessage());
			}
		});
		mRequestQueue.add(jr);
		hListViewCat.setAdapter(hListViewAdapterCat);
		hListViewCat.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				hListViewAdapterCat.setSelectIndex(position);
				hListViewAdapterCat.notifyDataSetChanged();
				PetDetail petDetail = (PetDetail)parent.getItemAtPosition(position);

				Bundle bundle=new Bundle();
				bundle.putString("petId", petDetail.petId);

				Intent intent=new Intent("android.intent.action.PETDETAIL");
				intent.putExtras(bundle);
				startActivity(intent);
//

			}
		});


		Button btn_more_dog=(Button)view.findViewById(R.id.btn_more_dog);
		//bt_second.setText(MainActivity.temp);
		btn_more_dog.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Bundle bundle=new Bundle();
				bundle.putString("petType", "dog");

				Intent intent=new Intent("android.intent.action.MORE");
				intent.putExtras(bundle);
				startActivity(intent);

			}
		});

		Button btn_more_cat=(Button)view.findViewById(R.id.btn_more_cat);
		//bt_second.setText(MainActivity.temp);
		btn_more_cat.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub


				Bundle bundle=new Bundle();
				bundle.putString("petType", "cat");

				Intent intent=new Intent("android.intent.action.MORE");
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});

		return view;
	}

	public static void popAllFragmentsExceptTheBottomOne()
	{
		for (int i = 0, count = fragmentManager.getBackStackEntryCount() - 1; i < count; i++)
		{
			Log.i("www-fragment1", Integer.toString(i));
			fragmentManager.popBackStack();
		}
	}
}
